from .base import BaseHandler
import sys
sys.path.append("..")
from db import sess
from models import User
import json
from views.func_tool import sms

# 管理员
class Admin(BaseHandler):
    def get(self, *args, **kwargs):
        self.render('../templates/index.html')


# 首页
class Welcome(BaseHandler):
    def get(self, *args, **kwargs):
        self.render('../templates/welcome.html')


# 登陆
class Login(BaseHandler):
    def get(self, *args, **kwargs):
        self.render('../templates/login.html')
    def post(self, *args, **kwargs):
        result = {}
        account = self.get_argument('account')
        password = self.get_argument('password')
        print(account,password)
        u = sess.query(User).filter(User.account == account).first()
        if u:
            if u.password == password:
                result['code'] = 200
                result['data'] = '成功！'
                self.write(json.dumps(result,ensure_ascii=False,indent=4))
                # self.write(json.dumps({"status":0,"msg":"返回成功","user_info":Person.__json__(p)},ensure_ascii=False,indent=4))
            else:
                result['code'] = 404
                result['data'] = '密码错误'
                self.write(json.dumps(result,ensure_ascii=False,indent=4))
        else:
            result['code'] = 404
            result['data'] = '用户不存在'
            self.write(json.dumps(result,ensure_ascii=False,indent=4))


# 查看用户
class User_list(BaseHandler):
    def get(self, *args, **kwargs):
        data = {}
        all = sess.query(User)
        data['all'] = all
        for i in all:
            print(i)
        print('ok')
        self.render('../templates/user-list.html',**data)


# 用户添加
class User_add(BaseHandler):
    def get(self, *args, **kwargs):
        self.render('../templates/member-add.html')
    def post(self, *args, **kwargs):
        username = self.get_argument('username')
        userphone = self.get_argument('userphone')
        useremail = self.get_argument('useremail')
        useraddress = self.get_argument('useraddress')
        userinfo = self.get_argument('userinfo')
        print(username,userphone,useraddress,useremail,userinfo)
        if not all([username,userphone,useremail,useraddress,userinfo]):
            self.write('信息不全')
        else:
            obj = User()
            obj.name = username
            obj.password = 123456
            # obj.gender = gender
            obj.phone = userphone
            obj.email = useremail
            obj.address = useraddress
            obj.social = userinfo
            sess.add(obj)
            sess.commit()
            self.redirect('/user')


# 用户删除
class User_del(BaseHandler):
    def get(self, *args, **kwargs):
        id = self.get_argument('id')
        user = sess.query(User).filter(User.id == id).first()
        sess.delete(user)
        sess.commit()
        self.redirect('/user')


# 用户批量删除
class User_dels(BaseHandler):
    def get(self, *args, **kwargs):
        # id = self.get_body_arguments('box')
        # user = sess.query(User).filter(User.id in id).first()
        # sess.delete(user)
        # sess.commit()
        self.redirect('/user')


# 用户修改
class Update(BaseHandler):
    def get(self, *args, **kwargs):
        self.render('../templates/update.html')
    def post(self, *args, **kwargs):
        id = self.get_argument('id')
        username = self.get_argument('username')
        gender = self.get_argument('sex')
        userphone = self.get_argument('userphone')
        useremail = self.get_argument('useremail')
        useraddress = self.get_argument('useraddress')
        userinfo = self.get_argument('userinfo')
        print(username,userphone,useraddress,useremail,gender,userinfo)
        if not all([username,gender,userphone,useremail,useraddress,userinfo]):
            self.write('信息不全')
        else:
            u = sess.query(User).filter(User.id == id).first()
            if u:
                u.name = username
                u.gender = gender
                u.phone = userphone
                u.email = useremail
                u.address = useraddress
                u.social = userinfo
                sess.commit()
                self.redirect('/user')